package com.estore.model;

import java.util.Objects;

/**
 * 商品实体类
 */
public class Product implements Comparable<Product> {

    private String id;      // 商品ID
    private String name;    // 商品名称
    private double price;   // 价格
    private int stock;      // 库存

    public Product(String id, String name, double price, int stock) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.stock = stock;
    }

    /* -------------------- Getters / Setters -------------------- */
    public String getId() { return id; }
    public void setId(String id) { this.id = id; }

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }

    public double getPrice() { return price; }
    public void setPrice(double price) { this.price = price; }

    public int getStock() { return stock; }
    public void setStock(int stock) { this.stock = stock; }

    /* -------------------- Object 方法 -------------------- */
    @Override
    public String toString() {
        return String.format("ID: %-5s | 名称: %-20s | 价格: ¥%-8.2f | 库存: %d", id, name, price, stock);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Product product = (Product) o;
        return Objects.equals(id, product.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    /* ==================== 排序规则 ==================== */
    @Override
    public int compareTo(Product other) {
        // 1. 按 ID 升序（默认）
        return this.id.compareTo(other.id);

        // 2. 按价格升序
        // return Double.compare(this.price, other.price);

        // 3. 按库存升序
        // return Integer.compare(this.stock, other.stock);
    }
}